<!--
  -    Copyright (c) 2018-2025, hycloud All rights reserved.
  -
  - Redistribution and use in source and binary forms, with or without
  - modification, are permitted provided that the following conditions are met:
  -
  - Redistributions of source code must retain the above copyright notice,
  - this list of conditions and the following disclaimer.
  - Redistributions in binary form must reproduce the above copyright
  - notice, this list of conditions and the following disclaimer in the
  - documentation and/or other materials provided with the distribution.
  - Neither the name of the pig4cloud.com developer nor the names of its
  - contributors may be used to endorse or promote products derived from
  - this software without specific prior written permission.
  - Author: hycloud
  -->
  <template>
    <div class="execution">
        <basic-container>
            <avue-crud ref="crud" :page.sync="page" :data="tableData" :permission="permissionList"
                :table-loading="tableLoading" :option="tableOption" @on-load="getList" @search-change="searchChange"
                @refresh-change="refreshChange" @size-change="sizeChange" @current-change="currentChange"
                @row-update="handleUpdate"
                       @row-save="handleSave"
                       @row-del="rowDel">
            </avue-crud>
        </basic-container>
    </div>
</template>

<script>
import RenderForm from '@/components/table-design/newRender'
import addView from '@/mixins/addView'
import { mapGetters } from 'vuex'


function getRandomString(length) {
  const characters = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';
  let result = '';
  for (let i = 0; i < length; i++) {
    const randomIndex = Math.floor(Math.random() * characters.length);
    result += characters.charAt(randomIndex);
  }
  return result;
}

export default {
    components: { RenderForm },
    mixins: [addView],
    name: 'alarmsettings',
    data() {
        return {
            formObj: {
                code: 'alarmsettings',
                title: '',
                flag: 'start',
                bizData: {}
            },
            bizId: '',
            unitId: '',
            unitName: '',
            validityDate: '',
            modal: false,
            modal1: false,
            modal2: false,
            modal3: false,
            searchForm: {},
            tableData: [ {
                id:1,
                garrisonName: '1F',
                area:"会议室",
                startTime: '2024-01-01 00:00:00',
                endTime: '2024-12-30 00:00:00',
                status: 0
            }, {
                id:2,
                garrisonName: '2F',
                area:"茶水间",
                startTime: '2024-01-01 00:00:00',
                endTime: "",
                status: 1
            }
            ],
            page: {
                total: 0, // 总页数
                currentPage: 1, // 当前页数
                pageSize: 20 // 每页显示多少条
            },
            tableLoading: false,
            tableOption: {
                "border": true,
                "index": true,
                "indexLabel": "序号",
                "stripe": true,
                "menuAlign": "center",
                "align": "center",
                "dialogClickModal": false,
                "searchMenuSpan": 6,
                "viewBtnIcon":'el-icon-view',
                "viewBtnText": '查看',
                editBtnText: '编辑',
                addBtn:true,
                viewBtn:true,

                "column": [
                    {
                        "type": "input",
                        "label": "id",
                        "prop": "id",
                        width: 500,
                        search: true,
                        hide:true,
                        editDisplay:false,
                        addDisplay:false,
                        viewDisplay:false,
                    },
                    {
                        "type": "input",
                        "label": "防区名称",
                        "prop": "garrisonName",
                        width: 500,
                        search: true,
                    },
                    {
                        "type": "input",
                        "label": "区域",
                        "prop": "area",
                        width: 500,
                        search: true,
                    },
                    {
                        "type": "datetime",
                        "label": "开始时间",
                        "prop": "startTime",
                        searchLabelWidth: 150,
                        width: 500,
                        search: true,
                    },
                    {
                        "type": "datetime",
                        "label": "结束时间",
                        "prop": "endTime",
                        searchLabelWidth: 150,
                        width: 500,
                        search: true,
                    },
                    {
                        "type": "select",
                        "label": "布防状态",
                        "prop": "status",
                        searchLabelWidth: 150,
                        width: 500,
                        search: true,
                        dataType: 'number',
                        dicData: [{
                            label: '撤防',
                            value: 0
                        },{
                            label: '布防',
                            value: 1
                        }],
                    }]
            }
        }
    },
    computed: {
        ...mapGetters(['permissions', 'tagList']),
        permissionList() {
            return {
                addBtn: true,
                delBtn: true,
                editBtn: true
            };
        }
    },
    methods:{
        rowDel: function (row, index) {

                var vc =this;
                this.$confirm('是否确认删除?', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(function () {
                    index=vc.tableData.findIndex(item=>item.id==row.id)
                    vc.$delete(vc.tableData,index)
                    vc.$message.success('删除成功')

                })
            },
            handleUpdate: function (row, index, done,loading) {

                var index=this.tableData.findIndex(item=>item.id==row.id)
                
                this.tableData[index]=row;

                this.$set(this.tableData,index,row)

                this.$message.success('修改成功')
                    done()
            },
            handleSave: function (row, done,loading) {

                row.id=getRandomString(8);
                this.tableData.push(row)
                this.$message.success('添加成功')
                this.$message.success('添加成功')
                done()
            }
    }
}
</script>


<style lang="scss">
.el-radio {
    color: #000000;
}
</style>
